<?php

use PHPUnit\Framework\TestCase;
use Nece\Sdk\Coze\Api\Conversation;

class ConversationTest extends TestCase
{
    /**
     * @var Conversation
     */
    protected $conversation;

    protected function setUp(): void
    {
        $client = Bootstrap::getCozeClient();
        $this->conversation = $client->conversation();
    }

    /**
     * 测试创建会话
     *
     * @author gjw
     * @created 2025-04-24 13:44:27
     *
     * @return void
     */
    public function testCreate(){
        $result = $this->conversation->create(Bootstrap::$bot_id);

        $this->assertIsArray($result);
        $this->assertArrayHasKey('code', $result);
        $this->assertEquals(0, $result['code']);
        $this->assertArrayHasKey('id', $result['data']);

        // 用@depends testCreate方式传递给下一个测试
        return $result['data']['id']??'';
    }

    /**
     * 测试查看会话信息
     *
     * @author gjw
     * @created 2025-04-24 14:01:09
     * @depends testCreate
     *
     * @param string $conversation_id
     * @return void
     */
    public function testRetrieve($conversation_id){
        $result = $this->conversation->retrieve($conversation_id);
        
        $this->assertIsArray($result);
        $this->assertArrayHasKey('code', $result);
        $this->assertEquals(0, $result['code']);
    }

    /**
     * 测试查看会话列表
     * 
     * @author gjw
     * @created 2025-04-24 13:42:31
     *
     * @return void
     */
    public function testConversations()
    {
        $result = $this->conversation->conversations(Bootstrap::$bot_id);
        $this->assertIsArray($result);
        $this->assertArrayHasKey('code', $result);
        $this->assertEquals(0, $result['code']);
    }

    /**
     * 测试清除上下文
     *
     * @author gjw
     * @created 2025-04-24 14:00:44
     * @depends testCreate
     *
     * @param string $conversation_id
     * @return void
     */
    public function testClear($conversation_id){
        $result = $this->conversation->clear($conversation_id);
        
        $this->assertIsArray($result);
        $this->assertArrayHasKey('code', $result);
        $this->assertEquals(0, $result['code']);
    }

    /**
     * 测试创建消息
     *
     * @author gjw
     * @created 2025-04-24 14:04:21
     * @depends testCreate
     *
     * @param string $conversation_id
     * @return void
     */
    public function testMessageCreate($conversation_id){
        $result = $this->conversation->messageCreate($conversation_id, 'user', '这是一个测试', 'text');
        // print_r($result);

        $this->assertIsArray($result);
        $this->assertArrayHasKey('code', $result);
        $this->assertEquals(0, $result['code']);

        return $result['data']['id'];
    }

    /**
     * 测试查看消息列表
     *
     * @author gjw
     * @created 2025-04-24 14:05:34
     * @depends testCreate
     *
     * @param string $conversation_id
     * @return void
     */
    public function testMessageList($conversation_id) {
        $result = $this->conversation->messageList($conversation_id);
        
        $this->assertIsArray($result);
        $this->assertArrayHasKey('code', $result);
        $this->assertEquals(0, $result['code']);
    }

    /**
     * 测试查看消息详情
     *
     * @author gjw
     * @created 2025-04-24 14:07:03
     * @depends testCreate
     * @depends testMessageCreate
     *
     * @param string $conversation_id
     * @param string $message_id
     * @return void
     */
    public function testMessageRetrieve($conversation_id, $message_id){
        $result = $this->conversation->messageRetrieve($conversation_id, $message_id);
        
        $this->assertIsArray($result);
        $this->assertArrayHasKey('code', $result);
        $this->assertEquals(0, $result['code']);
    }

    /**
     * 测试修改消息
     *
     * @author gjw
     * @created 2025-04-24 14:08:55
     * @depends testCreate
     * @depends testMessageCreate
     *
     * @param string $conversation_id
     * @param string $message_id
     * @return void
     */
    public function testMessageModify($conversation_id, $message_id){
        $result = $this->conversation->messageModify($conversation_id, $message_id, '这是一个消息的修改', 'text');

        $this->assertIsArray($result);
        $this->assertArrayHasKey('code', $result);
        $this->assertEquals(0, $result['code']);
    }
    
    /**
     * 测试删除消息
     *
     * @author gjw
     * @created 2025-04-24 14:08:55
     * @depends testCreate
     * @depends testMessageCreate
     *
     * @param string $conversation_id
     * @param string $message_id
     * @return void
     */
    public function testMessageDelete($conversation_id, $message_id){
        $result = $this->conversation->messageDelete($conversation_id, $message_id);
        
        $this->assertIsArray($result);
        $this->assertArrayHasKey('code', $result);
        $this->assertEquals(0, $result['code']);
    }
}